package com.webservice.client;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;

/**
 * @author HelloDream
 */
@Component
public class  ClientDemo{

    /**
     * 调用Webservice接口
     * @param url 接口地址 http://localhost:8080/mytest/TFamilyService?wsdl
     * @param interfaceName 接口名称 chairman
     * @param param 参数
     * @return
     */
    public String callWebserviceInterface(String url, String interfaceName,String param) {
        String result = null;
        try {
            Call call;
            result = "";
            call = getServiceCall(url, interfaceName);
            // 调用接口
            result = (String)call.invoke(new Object[] {param});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private Call getServiceCall(String url, String interfaceName) throws Exception {
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTargetEndpointAddress(new URL(url));
        // wsdl中 空间名称为  http://service.webservice.com 接口名称为 interfaceName
        call.setOperationName(new QName("http://service.webservice.com", interfaceName));
        // 添加参数 reqXml为wsdl中定义的参数名称
        call.addParameter("reqXml", XMLType.XSD_STRING, ParameterMode.IN);
        //  设置返回类型
        call.setReturnType(XMLType.XSD_STRING);
        return call;
    }

}
